/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.management;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.ui.entity.management.DepartmentBean;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.NaizhuAILog;

import java.util.List;

public class StructureListAdapterHolder extends RecyclerView.Adapter {

    public static final int STRUC_TYPE_TEAM = 0;
    public static final int STRUC_TYPE_PEOPLE = 1;
    private int listType;

    private List<DepartmentBean> teamLists;
    private List<PersonUserBean> oddPeopleLists;
    private OnItemClickListener mItemClickListener;
    private Context mContext;

    public StructureListAdapterHolder(Context context, List<DepartmentBean> teamLists, List<PersonUserBean> oddPeopleLists, int listType) {
        this.mContext = context;
        this.teamLists = teamLists;
        this.oddPeopleLists = oddPeopleLists;
        this.listType = listType;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == STRUC_TYPE_TEAM) {
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_structure_team_item, parent, false);
            return new TeamViewHolder(sView);
        } else {
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_structure_oddpeople_item, parent, false);
            return new PeopleViewHolder(sView);
        }
    }

    private int index = 0;

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TeamViewHolder) {
            index++;
            TeamViewHolder topHolder = (TeamViewHolder) holder;
            topHolder.tvTeamName.setText(teamLists.get(position).getDepartmentName());
            topHolder.departNumber.setText(String.format(mContext.getString(R.string.no_one_number),
                    String.valueOf(teamLists.get(position).getDepartmentCount())));
        } else if (holder instanceof PeopleViewHolder) {
            PeopleViewHolder contentHolder = (PeopleViewHolder) holder;
            contentHolder.tvPersonName.setText(oddPeopleLists.get(position).getShowName());
            ImageUtils.glideThumbViewByUrl(mContext, contentHolder.mvPersonLogo, oddPeopleLists.get(position).getUserLogoUrl());
        }
    }

    @Override
    public int getItemViewType(int position) {
        return listType;
    }

    @Override
    public int getItemCount() {
        if (listType == STRUC_TYPE_TEAM)
            return teamLists.size();
        else
            return oddPeopleLists.size();
    }

    public class TeamViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView tvTeamName;
        TextView departNumber;

        public TeamViewHolder(View view) {
            super(view);
            tvTeamName = (TextView) view.findViewById(R.id.struc_team_name);
            departNumber = (TextView) view.findViewById(R.id.department_number);
            view.setOnClickListener(this);
        }


        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, getAdapterPosition(), STRUC_TYPE_TEAM);
            }
        }
    }


    public class PeopleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView mvPersonLogo;
        TextView tvPersonName;

        public PeopleViewHolder(View view) {
            super(view);
            mvPersonLogo = (ImageView) view.findViewById(R.id.struc_user_head_logo);
            tvPersonName = (TextView) view.findViewById(R.id.struc_user_name);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, getAdapterPosition(), STRUC_TYPE_PEOPLE);
            }
        }
    }


    public interface OnItemClickListener {
        public void onItemClick(View view, int position, int listType);
    }

    public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

}
